package com.gw.quotes.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gw.quotes.R;
import com.gw.quotes.activity.NoticeDetailActivity;
import com.gw.quotes.entity.business.GetInformationListEntity.Information;
import com.gw.quotes.util.DateUtil;

public class NoticeAdapter extends BaseAdapter {

	private ArrayList<Information> list;
	private Context context;
	private int resource;

	public NoticeAdapter(Context context, int layoutId,
			ArrayList<Information> list) {
		this.context = context;
		this.resource = layoutId;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list == null ? 0 : list.size();
	}

	@Override
	public Object getItem(int position) {
		return list == null ? null : list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Information notice = list.get(position);
		LinearLayout layout = null;
		if (convertView == null) {
			layout = (LinearLayout) LayoutInflater.from(this.context).inflate(
					resource, null);
		} else {
			layout = (LinearLayout) convertView;
		}
		TextView time = (TextView) layout.findViewById(R.id.notice_time);
		TextView title = (TextView) layout.findViewById(R.id.notice_title);
		TextView content = (TextView) layout.findViewById(R.id.notice_content);
		RelativeLayout noticeDetailed = (RelativeLayout) layout
				.findViewById(R.id.notice_det_layout);
		title.setText(notice.getAuthor());
		content.setText(notice.getTitle());
		time.setText(DateUtil.formatDate("yyyy-MM-dd",
				notice.getPublishtime()));
		noticeDetailed.setOnClickListener(new MyListener(position));
		return layout;
	}

	private class MyListener implements OnClickListener {
		int mPosition;

		public MyListener(int inPosition) {
			mPosition = inPosition;
		}

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(context, NoticeDetailActivity.class);
			intent.putExtra("itemInfo", list.get(mPosition));
			context.startActivity(intent);
		}

	}

}
